#ifndef LOG_HPP
#define LOG_HPP

#include<iostream>
#include<cstdio>
#include<cstdarg>
#include<string>
#include<ctime>
// 日志是有日志级别的
//#define DEBUG   0
//#define NORMAL  1
//#define WARNING 2
//#define ERROR   3
//#define FATAL   4

#define LOGFILE "./calculator.log"
enum
{
    DEBUG,
    NORMAL,
    WARNING,
    ERROR,
    FATAL,
};

const char *gLevelMap[] = {
    "DEBUG",
    "NORMAL",
    "WARNING",
    "ERROR",
    "FATAL"
};

void logMessage(int level, const char* format, ...)
{
    #ifdef DEBUG_SHOW
    if(level == DEBUG)
        return;
    #endif
    
    //va_list ap;
    //va_start(ap, format);
    //while(...)
    //int x = va_arg(ap, int);
    //va_end(ap);

    char stdBuffer[1024];
    time_t timestamp = time(nullptr);
    struct tm *lt = localtime(&timestamp);
    snprintf(stdBuffer, sizeof stdBuffer, "[%s] [%d] [%d] ", gLevelMap[level], lt->tm_mday, lt->tm_hour);

    char logBuffer[1024];
    va_list ap;
    va_start(ap, format);
    vsnprintf(logBuffer, sizeof logBuffer, format, ap);
    va_end(ap);

    FILE *fp = fopen(LOGFILE, "a");
    // printf("%s%s\n", stdBuffer, logBuffer);
    fprintf(fp, "%s%s\n", stdBuffer, logBuffer);
    fclose(fp);
}


#endif
